package com.share.example.bcs.client.client.tcp;

import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.CharsetUtil;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class TcpClientHandler extends ChannelInboundHandlerAdapter {
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object obj) throws Exception {
        Channel channel = ctx.channel();

        //1.读取数据
        byte[] inputData = (byte[]) obj;
        String content = new String(inputData);
        String ip = channel.remoteAddress().toString();
        log.info("reciveMsg ip={},lenth={},content={}", ip, content.length(), content);

//        业务业务处理


//        根据服务的入参执行是否继续
//        String outData = "服务端你好,我收到了你的响应";
//        channel.writeAndFlush(outData.getBytes(CharsetUtil.UTF_8));
    }

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        // 发送数据
        log.info("连接上了 服务器....");
        ctx.channel().writeAndFlush("我是客户端小王,服务端你好,很高兴认识你!".getBytes(CharsetUtil.UTF_8));
    }
}
